<?php

namespace common\models;

use common\behaviors\VenueAttributeBehavior;
use common\behaviors\UploadBehavior;
use mivans\serialized\SerializedAttributes;
use Yii;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "{{%venue}}".
 *
 * @property integer $id
 * @property integer $cate_id
 * @property integer $hotel_id
 * @property string $name
 * @property string $description
 * @property string $content
 * @property string $thumb_path
 * @property string $thumb_base_url
 * @property double $price
 * @property integer $view
 * @property integer $scheduled
 * @property integer $accommodate
 * @property double $acreage
 * @property string $size
 * @property integer $created_at
 * @property integer $updated_at
 */
class Venue extends \yii\db\ActiveRecord
{

    // 图片组
    public $attachments;

    // 缩略图
    public $thumb;

    // 属性
    public $venueAttributes;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%venue}}';
    }

    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
            [
                'class' => SerializedAttributes::className(),
                'attributes' => ['size'],
            ],
            [
                'class' => UploadBehavior::className(),
                'attribute' => 'attachments',
                'multiple' => true,
                'uploadRelation' => 'venuePhoto',
                'pathAttribute' => 'path',
                'baseUrlAttribute' => 'base_url',
                'typeAttribute' => 'type',
                'sizeAttribute' => 'size',
                'nameAttribute' => 'name',
                'orderAttribute' => 'order'
            ],
            [
                'class' => UploadBehavior::className(),
                'attribute' => 'thumb',
                'pathAttribute' => 'thumb_path',
                'baseUrlAttribute' => 'thumb_base_url',
            ],
            [
                'class' => VenueAttributeBehavior::className(),
                'attribute' => 'venueAttributes',
                'attributeRelation' => 'venueAttribute'
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['cate_id', 'hotel_id', 'view', 'scheduled', 'accommodate', 'created_at', 'updated_at'], 'integer'],
            [['hotel_id', 'cate_id', 'name', 'price'], 'required'],
            [['content', 'thumb_base_url', 'thumb_path'], 'string'],
            [['price', 'acreage'], 'number'],
            [['name'], 'string', 'max' => 100],
            [['description'], 'string', 'max' => 255],
            [['size', 'attachments', 'thumb', 'venueAttributes'], 'safe']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'cate_id' => '分类',
            'hotel_id' => '酒店',
            'name' => '会场名称',
            'description' => '简介',
            'content' => '内容',
            'thumb' => '缩略图',
            'price' => '价格',
            'view' => '浏览数',
            'scheduled' => '预定数',
            'accommodate' => '容纳人数',
            'acreage' => '面积',
            'size' => '场地尺寸',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'attachments' => '缩略图组'
        ];
    }

    /**
     * 关联分类
     * @return \yii\db\ActiveQuery
     */
    public function getCategory()
    {
        return $this->hasOne(Category::className(), ['id' => 'cate_id']);
    }

    /**
     * 关联酒店
     * @return \yii\db\ActiveQuery
     */
    public function getHotel()
    {
        return $this->hasOne(Hotel::className(), ['id' => 'hotel_id']);
    }

    /**
     * 关联图片组
     * @return \yii\db\ActiveQuery
     */
    public function getVenuePhoto()
    {
        return $this->hasMany(VenuePhoto::className(), ['venue_id' => 'id']);
    }

    /**
     * 关联产品属性
     * @return \yii\db\ActiveQuery
     */
    public function getVenueAttribute()
    {
        return $this->hasMany(VenueAttribute::className(), ['venue_id' => 'id']);
    }

    /**
     * 获取缩略图地址
     * @return string
     */
    public function getThumbUrl()
    {
        return $this->thumb_base_url.'/'.$this->thumb_path;
    }
}